package do_.com.bpd.business.referrals.impl;

import com.csvreader.CsvReader;
import do_.com.bpd.business.referrals.api.ICaseExtractor;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 *
 * @author Samuel Pichardo
 */
public class CaseExtractor implements ICaseExtractor {

    @Override
    public Map<Integer, Map<String, String>> getCase(File file) throws IOException {
        CsvReader reader = null;
        Map<Integer, Map<String, String>> result = new LinkedHashMap<Integer, Map<String, String>>();
        try {
            reader = new CsvReader(file.getPath());

            int rowCount = 0;

            //Obtener cabeceras
            reader.readRecord();
            int columnsHeads = reader.getColumnCount();
            String[] heads = new String[columnsHeads];

            for (int i = 0; i < columnsHeads; i++) {
                heads[i] = reader.get(i);
            }
            //



            while (reader.readRecord()) {
                Map<String, String> myrow = new HashMap<String, String>();
                int columns = reader.getColumnCount();

                for (int i = 0; i < columns; i++) {
                    String key = heads[i];
                    String value = reader.get(i);
                    myrow.put(key, value);
                }
                rowCount++;
                result.put(new Integer(rowCount), myrow);
            }
        } catch (IOException e) {
            throw e;
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
//        for(int a=0;a<result.size();a++)
//        {
//        String print=result.get
//        }
        return result;
    }
}